import java.util.Vector;

public class SimbolTable {
	private Vector Simbolos;

	public SimbolTable() {
		Simbolos = new Vector();
	}

	/* ver este metodo */

	private int getTokenPos(String id, String token) {
		for (int i = 0; i < Simbolos.size(); i++) {
			SimbolNode SN = (SimbolNode) Simbolos.get(i);
			if ((id.equals(SN.getId())) && (token.equals(SN.getToken()))) {
				return i;
			}
		}
		return -1;
	}

	public int insertToken(String id, String token,String tipo) {
		int pos = getTokenPos(id, token);
		if (pos == -1) {
			SimbolNode SN = new SimbolNode(id, token, tipo);
			Simbolos.add(SN);
		}
		else{
			SimbolNode SN = (SimbolNode) Simbolos.get(pos);
			SN.incCount();
		}
		return getTokenPos(id,token);
	}

	public Vector getSimbolos() {
		Vector aux = new Vector();
		for(int i=0;i<Simbolos.size();i++){
			SimbolNode SN = (SimbolNode) Simbolos.get(i);
			if(SN.getCount()>0)
				aux.add(SN);
		}
		return aux;
	}

	/* ver este metodo */

	public String toString() {
		String res = "";
		for (int i = 0; i < Simbolos.size(); i++) {
			res += "<" + i + ">: " + Simbolos.get(i).toString();
			res += System.getProperty("line.separator");
		}
		return res;
	}
	

	  public void addConstanteNeg(String token){
	    int pos = getTokenPos("Constante",token);
	    if (pos!=-1){
	      SimbolNode SN = (SimbolNode) Simbolos.get(pos);
	      if (SN.getCount()>0){
	        SN.decCount();
	        insertToken("Constante", '-' + token,SN.getTipo());
	      }
	    }
	    else{
	      SimbolNode SN = (SimbolNode) Simbolos.get(getTokenPos("Constante",'-'+token));
	      SN.incCount();
	    }

	  }
	  //nuevo
	  public SimbolNode getTokenAt(int x){
	    return (SimbolNode)Simbolos.get(x);
	  }

	  public void clearAll(){
	    Simbolos.clear();
	  }
}
